package com.tzxs.pay.main.bill.detail.view;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.tzxs.pay.R;
import com.tzxs.pay.main.bill.detail.navigator.MsgRefundNavigator;

import java.util.Objects;

public class RefundDialogFragment extends DialogFragment implements View.OnClickListener {


    public static final String TAG = RefundDialogFragment.class.getSimpleName();
    private TextView backMoney;//要退款的金额
    private TextView getCode;//获取短信验证码vie
    private EditText code;//输入的验证码w
    private TextView sure;//确认
    private TextView cancel;//取消

    private MsgRefundNavigator navigator;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        navigator = (MsgRefundNavigator) context;

    }

    //获取dialogFragment实例
    public static RefundDialogFragment newInstance() {
        Bundle args = new Bundle();
        RefundDialogFragment fragment = new RefundDialogFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onStart() {
        super.onStart();
        int width = Objects.requireNonNull(getContext()).getResources().getDisplayMetrics().widthPixels;
        Objects.requireNonNull(getDialog().getWindow()).setLayout((int) (width * 0.75), ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Objects.requireNonNull(getDialog().getWindow()).setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        return inflater.inflate(R.layout.msg_refund_dialog, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        //退款金额
        backMoney = view.findViewById(R.id.back_money);
        //输入的验证码
        code = view.findViewById(R.id.back_code);
        //获取短信验证码
        getCode = view.findViewById(R.id.get_verify_code);
        //确认
        sure = view.findViewById(R.id.sure);
        //取消
        cancel = view.findViewById(R.id.cancel);

        getCode.setOnClickListener(this);
        sure.setOnClickListener(this);
        cancel.setOnClickListener(this);

        getData();
    }


    //获取验证码
    public String getCode() {
        return code.getText().toString();
    }


    private void getData() {
        Bundle bundle = getArguments();
        if (bundle == null) {
            return;
        }
        //设置退款金额
        backMoney.setText(bundle.getString("backMoney"));

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.get_verify_code:
                navigator.getVerifyCode((TextView) v);
                break;
            case R.id.sure:
                navigator.sure();
                break;
            case R.id.cancel:
                navigator.cancel();
                break;
        }

    }


}
